package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ReadBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApReadBehavior;
import com.heima.model.common.Article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {
    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult readBehavior(ReadBehaviorDTO dto) {
        ApUser user = AppThreadLocalUtils.getUser();
        //获取行为实体
        ApBehaviorEntry entry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if(entry==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"获取行为实体信息失败");
        }
        //查询阅读文章是否存在
        Query query=Query.query(Criteria.where("entryId").is(entry.getId()).and("articleId").is(dto.getArticleId()));
        ApReadBehavior apReadBehaviors = mongoTemplate.findOne(query, ApReadBehavior.class);

        //判断阅读行为是否存在
        if (apReadBehaviors==null){
            apReadBehaviors=new ApReadBehavior();
            apReadBehaviors.setEntryId(entry.getId());
            apReadBehaviors.setArticleId(dto.getArticleId());
            apReadBehaviors.setCreatedTime(new Date());
            apReadBehaviors.setUpdatedTime(new Date());
            apReadBehaviors.setCount((short) 1);
            mongoTemplate.save(apReadBehaviors);
        }else{
            apReadBehaviors.setCount((short)(apReadBehaviors.getCount()+1));
            mongoTemplate.save(apReadBehaviors);
        }

        NewBehaviorDTO newBehavior = new NewBehaviorDTO();
        newBehavior.setType(NewBehaviorDTO.BehaviorType.VIEWS);
        newBehavior.setArticleId(dto.getArticleId());
        newBehavior.setAdd(1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(newBehavior));
        log.info("成功发送 文章点赞行为消息 , 消息内容: {}",newBehavior);
            return ResponseResult.okResult();
    }
}
